jsx3.require("jsx3.gui.Select");

/**
 * Perspective Selector.  This class extends the jsx3.gui.Select class, to allow specific plugin class instances to be
 * bound to the selection events in the select box.
 */
jsx3.Class.defineClass(
        "tibco.admin.PerspectiveSelect",
        jsx3.gui.Select,
        null,
        function (select) {

            /*
            * An object ot ue used as an associative array, with the indexes being the displayed strings in the
            * select box, and the values being the plugin class instances associated with those strings.
            * @private
            * @jsxobf-clobber
            */
            select.prototype.dynamicItems = new Object();
			select.prototype._index = "0";

            select.prototype.init = function(strName, vntLeft, vntTop, vntWidth, vntHeight) {
                this.jsxsuper(strName, vntLeft, vntTop, vntWidth, vntHeight);
            };

            /**
             * Registers the plugin container with this select box.
             *
             * @param pluginContainer {Object} the object to perform a callback on when a selection is made.  This
             *        object is expected to have an <code>itemSelected(pluginInstance)</code> function, and the
             *        instance of the plugin component for the selected item will be passed to it.
             */
            select.prototype.registerWithPluginContainer = function(pluginContainer) {
                this.pluginContainer = pluginContainer;
            }

            /**
             * Adds a string/Object pair to the select box.
             *
             * @param name {String} the text string to be displayed in the select box for this item
             * @param obj {Object} the plugin component instance to be returned when this item is selected
             */
            select.prototype.addItemToSelectBox = function(name, obj) {
                var selectDOC = this.getXML();
                var newRecord = new Object();
                newRecord.jsxtext = name;
                newRecord.jsxid = ++this._index;
                this.dynamicItems[newRecord.jsxid] = obj;
                this.insertRecord(newRecord, "jsxroot", true);
                return newRecord.jsxid
            }

            select.prototype.revertSelection = function() {
                this.ignoreSelects = true;
                this.setValue(this.lastSelection);
                this.currentSelection = this.lastSelection;
                this.lastSelection = null;
                this.ignoreSelects = false;
            }

            /**
             * Internal function.  Not to be called directly by other components.  When a selection is made in the select
             * box, it looks up the plugin component instance for that selection, and passes it back to the plugin
             * container via a callback.
             *
             * @param strRECORDID {String} The id of the selected item, for performing the lookup
             */
            select.prototype.publishMessage = function(strRECORDID) {
                if(!this.ignoreSelects) {
                try {
                    this.lastSelection = this.currentSelection;
                    this.pluginContainer.itemSelected(this.dynamicItems[strRECORDID]);
                    this.currentSelection = this.getValue();
                }
                catch(e) {
                    tibco.admin.Main.logException(e);
                }
                }
            }


            select.prototype.customize = function() {
                this.getXSL();
                this.setDefaultText(tibco.admin.Main.getServer().getDynamicProperty("@@selectDefaultText"));
            }

        });